/* Copyright (c) 2006 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is confidential property of 
 * Nordic Semiconductor. The use, copying, transfer or disclosure of such
 * information is prohibited except by express written agreement with 
 * Nordic Semiconductor.
 */

/** @file
 * Timer for the Wireless Desktop Protocol.
 * Sets up Timer2 to call WDP and FAP functions at the correct timing 
 * intervals.
 */

#include <Nordic\reg24lu1.h>
#include <stdint.h>

#include "nordic_common.h"
#include "alpp.h"

volatile uint16_t timer_cnt = 0;

void t2_init(uint16_t val)
{
  // Setup Timer2:
  // 1/12 prescaler, reload Mode 0, Stopped
  T2CON = BIT_4;

  T2 = CRC = ~((val * 4) / 3);
  T2I0 = 1; // Start Timer2
}

static void t2_interrupt(void) interrupt 5
{
  TF2 = 0;

  alpp_timer_isr_function();
  
  if(timer_cnt < 0xffff)
  {
    timer_cnt++;
  }
}

void alpp_modify_timer_period(void)
{
  T2 = ~((ALPP_TIMER_MODIFY_PERIOD*4) / 3);
  TF2 = 0;
}

